Helpful Information
 
 
Category: OOP
OOP -> abstract classes.

Today, I've been brushing up on my coding skills, so I decided to review the PHP manual (Object oriented Programming). Most of it came very easily to me, but I just do not get the point of abstract classes.

Heres what I know about them so far:


Any member declared abstract, the class itself must be declared as abstract
You can not instantiate an abstract class


Basically, from my understanding, abstract classes just hold methods and properties that MUST be defined in the child class is this correct? If not, can someone please explain to me what the point of abstract classes are and how to use them?

Any input is greatly appreciated, Thanks!

Abstract base classes (ABCs) have a couple primary uses:

1) As an interface, you can define a generic interface that derived classes must implement. That lets you treat multiple similar but different objects the same. Especially since not all languages have a specific interface construct.

2) It allows you to not define all the functions, so if you build a base class for say shapes and the base class can have the functions common to all types of shapes. But you could have a draw function that is abstract since that would be specific to the shape.

Best phrase I can think of is minimal.
While interfaces IMO have exceedingly more power than an extension ever will, abstracts are the perfect minimal implementation of an interface. If you have an interface with say a public function loadINI() method on it, and every class uses the same INI file, then it makes sense to use an abstract to provide this minimal implementation detail instead of having the exact method on every class implementing it. This is especially true if it makes use of a method that has unknown implementation at the time, allowing you're class to call this method; if there is no call to a required method, a concrete adapter may make more sense (an adapter provides non-implementation by throwing exceptions or providing no implementation details for the concrete class). You may also have a base class that does not implement a provided interface and still defines a method, and then later extend it and implement this method. That will also work, though the base is not itself considered a typeof the given interface.
Note that the above can be easily modified to create an unrecognized bug in PHP (as in, the PHP team threw out my bug report indicating normal behaviour) when it is not the case. If you use this class structure:


<?php

interface IInterface
{
public function test();
}

class Grandfather
{
private function __construct()
{
}

private function test()
{
printf("%s\n", __METHOD__);
}
}

class Father extends Grandfather implements IInterface
{
public function __construct()
{
printf("%s\n", __METHOD__);
}

public function test()
{
printf("%s\n", __METHOD__);
}
}

class Son extends Father
{
protected function __construct()
{
parent::__construct();
printf("%s\n", __METHOD__);
}

public static function createInstance()
{
return new self;
}

private function test()
{
parent::test();
printf("%s\n", __METHOD__);
}
}


?>

Grandfather must not implement the interface and declare the same method signature as private.
And attempt to use the test method on an instance of Son, it will succeed and throw an illegal access error. This is incorrect as the error should be thrown when the linking occurs for the Son to Father - a child cannot demote the access level of a parent. This bug chews on me so much.

It should also be said like all things relating to OOP, it is very easy to over do it. ABCs aren't needed all the time, you have to use care and consideration when designing your objects.










privacy (GDPR)